<template>
  <div class="account-set">
    <mt-cell title="积分记录" is-link to="/pointRecords"></mt-cell>
    <mt-cell title="关于我们" is-link to="/aboutUs"></mt-cell>
    <mt-cell title="检查更新" @click.native="checkUpdate" is-link></mt-cell>
    <mt-cell title="退出账号" @click.native="logout" is-link></mt-cell>
  </div>
</template>
<script>
export default {
  name: 'FriendChat',
  methods: {
    logout () {
      localStorage.removeItem('token')
      localStorage.removeItem('user')
      localStorage.removeItem('login')
      this.$cookies.remove('login')
      this.$router.push('/login')
    },
    checkUpdate () {
      if (!window.isApp) {
        window.location.href = 'http://156.245.160.20/yiqun.exe'
      } else {
        checkService()
      }
      function checkService () {
        window.plus.nativeUI.showWaiting('检测更新...')
        var xhr = new window.plus.net.XMLHttpRequest()
        xhr.onreadystatechange = function () {
          switch (xhr.readyState) {
            case 4:
              if (xhr.status === 200) {
                let res = JSON.parse(xhr.responseText)
                if (res.success) {
                  window.plus.runtime.getProperty(window.plus.runtime.appid, (inf) => {
                    if (inf.version !== res.data.version) {
                      if (window.plus.os.name === 'iOS') {
                        window.plus.runtime.openURL(res.data.ios)
                      } else if (window.plus.os.name === 'Android') {
                        downLoad(res.data.android)
                      }
                    } else {
                      window.plus.nativeUI.toast('无新版本可更新！')
                      window.plus.nativeUI.closeWaiting()
                    }
                  })
                }
              }
              break
            default :
              break
          }
        }
        xhr.open('GET', 'http://156.245.160.20/checkVersion')
        xhr.send()
        window.plus.nativeUI.closeWaiting()
      }
      function downLoad (wgtUrl) {
        window.plus.nativeUI.showWaiting('下载文件...')
        window.plus.downloader.createDownload(wgtUrl, { filename: '_doc/yiqun/download/', force: true }, function (d, status) {
          if (status === 200) {
            console.log('下载成功：' + d.filename)
            install(d)
          } else {
            window.plus.nativeUI.alert('下载失败！')
          }
          window.plus.nativeUI.closeWaiting()
        }).start()
      }
      function install (d) {
        window.plus.nativeUI.showWaiting('安装文件...')
        window.plus.runtime.install(d.filename, { force: true }, function () {
          window.plus.nativeUI.closeWaiting()
          console.log('安装文件成功！')
          window.plus.nativeUI.alert('应用资源更新完成！', function () {
            window.plus.runtime.restart()
          })
        }, function (e) {
          window.plus.nativeUI.closeWaiting()
          console.log('安装文件失败[' + e.code + ']：' + e.message)
          window.plus.nativeUI.alert('安装apk文件失败[' + e.code + ']：' + e.message)
        })
      }
    }
  },
  load: {
    dom () {
    },
    plus: () => {
    }
  }
}
</script>

<style lang="less">
.account-set {
  .mint-button--default {
    background: transparent;
    border: 0;
    box-shadow: 0 0 0 transparent
  }
}
</style>
